<%--
  Created by IntelliJ IDEA.
  News: XBB
  Date: 2021/2/18
  Time: 14:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css">

    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/font/dtreefont.css">
</head>
<body>
<div style="padding: 10px">

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>查询条件</legend>
    </fieldset>
    <form  class="layui-form">
        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">公告标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title"   placeholder="请输入公告标题" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">公告内容</label>
                <div class="layui-input-inline">
                    <input type="text" name="content"   placeholder="请输入公告内容" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">发布人姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="publisherName"   placeholder="请输入发布人姓名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" style="text-align: right">
                <button  class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm" lay-submit lay-filter="doSearch">查询
                </button>
                <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
                </button>
            </div>
        </div>
    </form>

    <table id="newsTable" lay-filter="newsTableFilter"></table>
</div>

<script id="toolbarnews" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="newsAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deleteSome">批量删除</button>
    </div>
</script>

<script id="barnews" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs layui-btn-radius" lay-event="viewNews">查看</a>
</script>

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">标题:</label>
            <div class="layui-input-block">
                <input type="hidden" name="id" id="newsId">
                <input type="hidden" name="createTime">
                <input type="hidden" name="publisherName">
                <input type="text" name="title" placeholder="请输入标题" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容:</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="contentArea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius" lay-filter="doSubmit" lay-submit="">&nbsp; 提交</button>
                <button type="reset" id="dataFrmResetBtn" class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">&nbsp; 重置</button>
            </div>
        </div>
    </form>
</div>

<%--查看公告的div--%>
<div id="viewNewsDiv" style="padding: 10px;display: none;">
    <h2 id="view_title" align="center"></h2>
    <hr>
    <div style="text-align: right">
        发布人：<span id="view_publisherName"></span><br/>
        发布时间：<span id="view_dateTime"></span>
    </div>
    <hr>
    <div id="view_content">
    </div>
</div>


<script type="text/javascript" src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
<script>

    var newsTab;
    var newsIndex;
    var title;

    layui.use(['form','layer','jquery','table','laydate','layedit'],function() {
        var form = layui.form, layer = layui.layer, $ = layui.jquery,table = layui.table ,laydate=layui.laydate,layedit=layui.layedit;

        //渲染时间
        laydate.render({
            elem:'#startTime'
            ,type:'datetime'
        })

        laydate.render({
            elem:'#endTime'
            ,type:'datetime'
        })

        //初始化富文本编辑器
        var editIndex; //建立编辑器

        newsTab = table.render({
            elem:"#newsTable"
            ,url:"${pageContext.request.contextPath}/sys/news/newsRightJson"
            ,cols:[[
                {type:'checkbox'}
                ,{field:'id',title:'公告ID',align:"center",sort:true}
                ,{field:'title',title:'公告标题',align:"center"}
                ,{field:'content',title:'公告内容',align:"center"}
                ,{field:'createTime',title:'发布日期',align:"center"}
                ,{field:'publisherName',title:'发布人',align:"center"}
                ,{fixed:'right',title:'操作',align:"center",toolbar:'#barnews',minWidth:250}
            ]]
            ,toolbar:'#toolbarnews'
            ,page:true
            ,limit:[5,10,15]
            ,limit:5
        })

        //给查询按钮添加事件
        form.on('submit(doSearch)',function (data) {

            newsTab.reload({
                where:data.field
                ,page:{curr:1}
            })

            return false;
        })

        //监听头部工具栏
        table.on('toolbar(newsTableFilter)',function (obj) {
            if(obj.event == "newsAdd"){
                newsAdd()
            }else if(obj.event == "deleteSome"){
                //获取选中的行
                var checkStatus = table.checkStatus(obj.config.id)
                if(checkStatus.data.length == 0){
                    layer.msg("至少选择一行数据")
                    return false
                }
                //获取所有选中的newsId
                var str = ""
                for(var i = 0;i<checkStatus.data.length;i++){
                    var news = checkStatus.data[i];
                    str += news.id + ","
                }
                deleteSome(str,checkStatus.data.length)
            }
        })

        function deleteSome(str,count) {
            layer.confirm("您确定要删除这"+count+"条数据吗？",{icon:2,title:'批量删除'},function(i){
                $.post("${pageContext.request.contextPath}/sys/news/deleteSomeNews",{"ids":str},function(r){
                    if(r.code == 0){
                        //删除成功
                        layer.msg(r.msg, {
                            offset: '15px'
                            ,icon: 1
                            ,time: 1000
                        }, function(){
                            // 刷新table
                            newsTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        //删除失败
                        layer.msg(r.msg)
                    }
                })
            })
        }

        //点击添加按钮的弹出
        function newsAdd() {
            newsIndex = layer.open({
                type:1
                ,title:"添加公告"
                ,area:['700px','700px']
                ,content:$('#saveOrUpdateDiv')
            })

            //清除表单的内容
            $("#dataFrm").get(0).reset()
            $("#newsId").val("")
            //渲染编辑器
            editIndex = layedit.build('contentArea')
            form.render()
            title = "添加"
        }

        //给弹出层的form表单submit按钮添加点击事件
        form.on('submit(doSubmit)',function (obj) {
            layedit.sync(editIndex);
            var params = $("#dataFrm").serialize();
            layer.confirm("您确定要"+title+"吗？",function () {
                $.post("${pageContext.request.contextPath}/sys/news/newsAddOrUpdate",params,function (r) {
                    if(r.code == 0){
                        layer.msg(r.msg,{
                            offset:'15px'
                            ,icon:1
                            ,time:1000
                        },function () {
                            //关闭当前弹出框
                            layer.close(newsIndex)
                            //刷新table
                            newsTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        layer.msg(r.msg)
                    }

                })
            })
            return false;
        })

        //监听行工具栏
        table.on('tool(newsTableFilter)',function (obj) {
            if(obj.event == "edit"){
                edit(obj.data)
            }else if(obj.event == "del"){
                del(obj.data)
            }else if(obj.event == "viewNews"){
                viewNews(obj.data)
            }
        })

        //点击查看
        function viewNews(data) {
            newsIndex = layer.open({
                type:1
                ,title:"查看公告"
                ,area:['700px','500px']
                ,content:$('#viewNewsDiv')
            })
            $("#view_title").html(data.title)
            $("#view_content").html(data.content)
            $("#view_dateTime").html(data.createTime)
            $("#view_publisherName").html(data.publisherName)
            //给表单设置值
            form.render()
        }

        $("#dataFrmResetBtn").click(function(){
            layedit.setContent(editIndex,"");
        });

        //点击编辑
        function edit(data) {
            newsIndex = layer.open({
                type:1
                ,title:"修改公告"
                ,area:['700px','600px']
                ,content:$('#saveOrUpdateDiv')
            })

            //给表单设置值
            form.val('dataFrm',data)

            //渲染编辑器
            editIndex = layedit.build('contentArea')

            form.render()
            title = "修改"
        }

        //点击删除
        function del(data){
            layer.confirm("确定删除"+data.title+"吗？",function (i) {
                $.post("${pageContext.request.contextPath}/sys/news/newsDelete",{"id":data.id},function (r) {
                    if(r.code == 0){
                        layer.msg(r.msg,{
                            offset:'15px'
                            ,icon:1
                            ,time:1000
                        },function () {
                            //关闭当前弹出框
                            layer.close(newsIndex)
                            //刷新table
                            newsTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        layer.msg(r.msg)
                    }

                })
            })
        }
    })
</script>
</body>
</html>

